package cn.yangtianxia.org.service.impl;

import cn.yangtianxia.basic.exception.BusinessException;
import cn.yangtianxia.basic.service.Impl.BaseServiceImpl;
import cn.yangtianxia.basic.util.BaiduAuditUtils;
import cn.yangtianxia.basic.vo.ShopVo;
import cn.yangtianxia.org.domain.Employee;
import cn.yangtianxia.org.domain.Shop;
import cn.yangtianxia.org.domain.ShopAuditLog;
import cn.yangtianxia.org.mapper.ShopMapper;
import cn.yangtianxia.org.service.IEmployeeService;
import cn.yangtianxia.org.service.IShopAuditLogService;
import cn.yangtianxia.org.service.IShopService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yangtianxia
 * @since 2022-03-28
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IShopAuditLogService shopAuditLogService;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        String shopName = shop.getName();
        //百度ai智能审核
        if(!BaiduAuditUtils.TextCensor(shopName)){
          throw new BusinessException("店铺名为非法字符，请重新设置");
        }
        //参数不能为空
        if (StringUtils.isEmpty(shopName)){
            throw new BusinessException("店铺名称不能为空");
        }
        Shop tempShop = shopMapper.loadByName(shopName);
        //判断店铺名是否已经存在
        if (tempShop != null){
            throw  new BusinessException("商铺已经存在，请直接登录");
        }
        //传入的管理员对象
        Employee admin = shop.getAdmin();

        Employee empTemp = employeeService.loadByUserName(admin.getUsername());
        //管理员是否已经注册，并且绑定了店铺
        if (empTemp != null && admin.getShop_id() != null){
                throw new BusinessException("管理员已经存在并且绑定了店铺，请重新设置");
        }
        //管理员是否已经注册，但是为绑定店铺
        if (empTemp == null){
            //添加
            employeeService.save(admin);
            empTemp = admin;
        }
        //店铺保存
        shop.setAdminId(empTemp.getId());
        //添加店铺
        shopMapper.save(shop);
        //修改员工
        empTemp.setShop_id(shop.getId());
        employeeService.update(empTemp);
    }

    /**
     * 通过的情况
     * 修改state状态改为激活
     * 生成店铺审核日志
     * 并发送激活邮件
     * @param log
     */
    @Override
    @Transactional
    public void pass(ShopAuditLog log) {
        //修改状态
        Shop shop = shopMapper.loadById(log.getShopId());
        shop.setState(2);
        shopMapper.update(shop);
        //现在在操作审核店铺的时候没有管理员登录，无法获取内部审核人员信息写死id = 325
        log.setAuditId(325L);
        shopAuditLogService.save(log);
        //发送邮件
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("ll2441809962@163.com");
        message.setTo("ll2441809962@163.com");
        message.setSubject("宠物乐园激活请求");
        message.setText("亲，你的店铺已经初步审核通过，请手动完成激活并使用。激活链接：http://localhost:8080/shop/audit/"+log.getShopId());
        javaMailSender.send(message);
    }

    /**
     * 驳回的情况
     * 修改state状态改为驳回
     * 生成店铺审核日志
     * 并发送新的注册界面链接
     * @param log
     */
    @Override
    @Transactional
    public void reject(ShopAuditLog log) {
        //修改状态
        Shop shop= shopMapper.loadById(log.getShopId());
        shop.setState(-1);
        shopMapper.update(shop);
        //现在在操作审核店铺的时候没有管理员登录，无法获取内部审核人员信息写死id = 325
        log.setAuditId(325L);
        shopAuditLogService.save(log);
        //发送邮件
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("ll2441809962@163.com");
        message.setTo("ll2441809962@163.com");
        message.setSubject("驳回，请修改店铺信息，再次提交审核");
        message.setText("<h1>审核失败</h1><br>"+
                "<span>失败原因："+log.getNote()+"</span><br>"+
                "<a href='http://127.0.0.1:8081/#/register2?id="+log.getId()+"'>点击修改资料</a>");
        javaMailSender.send(message);
    }

    @Override
    @Transactional
    public void batchAdd(List<Shop> list) {
        shopMapper.batchAdd(list);
    }

    @Override
    public List<ShopVo> loadCount() {
        return shopMapper.loadCount();
    }

    @Override
    public List<Shop> queryAll() {
        return shopMapper.queryAll();
    }

}
